import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList:[], // 购物车数据
  },
  mutations: {
    saveCart(state,val){
      let index = state.cartList.findIndex(item => item.id == val.id);
      let obj = {
        ...val,
        buyNum: 1,
        check: false
      }
      if (index>-1){
        state.cartList[index].buyNum++;
        return
      }
      state.cartList.push(obj);
    },

    checkAll(state,bool){ // 全选的函数
      // console.log(bool);
      state.cartList.forEach(item => {
        item.check = bool
      })
    },
    delect(state){ // 批量删除
      state.cartList = state.cartList.filter(item => item.check == false);
    },
    kong(){
      
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
